#include "adapterselector.h"
#include "ui_adapterselector.h"

AdapterSelector::AdapterSelector(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AdapterSelector)
{
    ui->setupUi(this);
    
    // 设置对话框为模态
    setModal(true);
    
    // 初始状态下监听按钮不可用
    ui->listenButton->setEnabled(false);
}

AdapterSelector::~AdapterSelector()
{
    delete ui;
}

void AdapterSelector::setAvailableAdapters(const QList<QPair<QString, QString>>& adapters)
{
    m_adapters = adapters;
    
    // 清空列表
    ui->adapterList->clear();
    
    // 添加适配器到列表中
    for (const auto& adapter : adapters) {
        ui->adapterList->addItem(adapter.first + " - " + adapter.second);
    }
    
    // 如果有适配器，默认选择第一个
    if (!adapters.isEmpty()) {
        ui->adapterList->setCurrentRow(0);
        m_selectedAdapter = adapters.first().first;
        ui->adapterInfo->setPlainText("设备名: " + adapters.first().first + "\n" +
                                      "描述: " + adapters.first().second);
        ui->listenButton->setEnabled(true);
    }
}

QString AdapterSelector::getSelectedAdapter() const
{
    return m_selectedAdapter;
}

void AdapterSelector::on_listenButton_clicked()
{
    accept();
}

void AdapterSelector::on_adapterList_itemSelectionChanged()
{
    int currentIndex = ui->adapterList->currentRow();
    if (currentIndex >= 0 && currentIndex < m_adapters.size()) {
        m_selectedAdapter = m_adapters[currentIndex].first;
        ui->adapterInfo->setPlainText("设备名: " + m_adapters[currentIndex].first + "\n" +
                                      "描述: " + m_adapters[currentIndex].second);
        ui->listenButton->setEnabled(true);
    } else {
        ui->listenButton->setEnabled(false);
    }
}